require 'net/http'
require 'md5'
require 'iconv'

class KjMe2day
def initialize(usrname, key)
	@usrname = usrname
	@key = key
end
private
def getnonce(len)
  charr = ('A'[0]..'Z'[0]).to_a
  ret = []
  len.times do |x|
    ret.push charr[rand*100%charr.length].chr
  end
  ret.to_s
end
private
def getauthenticationstr
  nonce = getnonce(8)
  ret = nonce + MD5.hexdigest(nonce + @key)
  ret
end
public
def posting(postingstr)
  url = URI.parse("http://me2day.net/api/create_post/#{@usrname}.xml")
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth @usrname, getauthenticationstr
  req.set_form_data({'post[body]'=>postingstr},';')
  res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
  res.body
end
end

me2 = KjMe2day.new('kangjin', 'e50ca92b')
me2.posting()
